﻿
using Microsoft.EntityFrameworkCore;

namespace LeaderCCSLaserUI.Data
{
    public class MDbContext : DbContext
    {
        private string? DbPath;
        public DbSet<XYZPoint> XYZRPoints { get; set; }
        public DbSet<FlyGrabPoint> FlyGrabPoints { get; set; }
        public DbSet<MParam> MParams { get; set; }
        public DbSet<VisionPoint> VisionPoints { get; set; }
        public DbSet<ParamScale> ParamScales { get; set; }
        public DbSet<BarcodePoint> BarcodePoints { get; set; }
        public DbSet<VisionProgram> VisionPrograms { get; set; }
        public DbSet<VisionFunction> VisionFunctions { get; set; }
        public DbSet<VisionProject> VisionProjects { get; set; }
        public DbSet<ZControlPoint> ZControlPoints { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder options)
        {
            options.UseSqlite($"Data Source={DbPath};Pooling=False");
        }
        public MDbContext()
        {
            var path = System.AppDomain.CurrentDomain.BaseDirectory;
            DbPath = System.IO.Path.Join(path, "point.db");
        }
        public MDbContext(string part)
        {
            var path = System.AppDomain.CurrentDomain.BaseDirectory;
            DbPath = System.IO.Path.Join(path, part, "point.db");
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<VisionPoint>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Name).IsRequired(true).HasMaxLength(32);
            });
            modelBuilder.Entity<XYZPoint>(entity =>
            {
                entity.Property(e => e.Name).IsRequired(true).HasMaxLength(32);
                entity.Property(e => e.Alias).IsRequired(false).HasMaxLength(64);
            });
            modelBuilder.Entity<MParam>(entity =>
            {
                entity.Property(e => e.Name).IsRequired(true).HasMaxLength(32);
                entity.Property(e => e.Value).IsRequired(true);
                entity.Property(e => e.Hand).IsRequired(true);
            });
            modelBuilder.Entity<MParam>().HasData(new MParam
            {
                Id = 1,
                Name = "WeldZ",
                Type = typeof(double).Name,
                Value = "0",
                Hand = 0
            });
            modelBuilder.Entity<MParam>().HasData(new MParam
            {
                Id = 2,
                Name = "WeldH",
                Value = "260",
                Type = typeof(double).Name,
                Hand = 0
            });
            modelBuilder.Entity<MParam>().HasData(new MParam
            {
                Id = 3,
                Name = "WeldZ",
                Value = "0",
                Type = typeof(double).Name,
                Hand = 1
            });
            modelBuilder.Entity<MParam>().HasData(new MParam
            {
                Id = 4,
                Name = "WeldH",
                Value = "260",
                Type = typeof(double).Name,
                Hand = 1
            });
        }
    }
}
